<?php
/**
 * 基础设置
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;
use Model\SettingMod;

class Setting extends Action
{
	private $Setting = null;
	public function __construct()
	{
		parent::__construct();
		$this->Setting = new SettingMod();
	}

	/**
	 * 后台获取数据
	 */
	public function index()
	{
		$_result = $this->Setting->findOne(array('uniacid'=>$this->_uniacid));
        if(!$_result){
            $this->Setting->insert(array('uniacid'=>$this->_uniacid));
            $this->index();
        }
		$this->_result = array('status'=>'success','data'=>$_result);
		$this->result($this->_result);
	}

	/**
	 * 设置参数
	 * data数据
	 */
	public function set()
	{
		$_result = $this->Setting->set($this->_gpc['data']);
		if($_result){
			$this->_result = array('status'=>'success');
		}else{
			$this->_result = array('status'=>'error');
		}
		$this->result($this->_result);
	}

	/**
	 * 上传证书
	 */
	public function uploadPem()
	{
		$_file = array();
		foreach($_FILES['myFile'] as $_key=>$_value){
			for($i=0;$i<count($_FILES['myFile']['name']);$i++){
				$_file[$i][$_key] = $_value[$i];
			}
		}
		if(count($_file) > 2){
			$this->result(array('status'=>'error','err_status'=>4,'err_msg'=>'上传文件个数不能大于2'));
		}
		load()->func('file');
		foreach($_file as $_v){
			if(($_v['size']/1024) > 2){
				$this->result(array('status'=>'error','err_status'=>2,'err_msg'=>'证书大小不能超过2kb'));
			}
            $_filename = (strpos($_v['name'],'key.pem') === false) ? 'cert.pem' : 'key.pem';
			if(!file_move($_v['tmp_name'],ATTACHMENT_ROOT.'/kuyuan_vbz/'.$this->_w['account']['uniacid'].'/wx_cert/'.$_filename)){
				$this->result(array('status'=>'error','err_status'=>3,'err_msg'=>'上传失败，未知原因'));
			}
		}
		$this->result(array('status'=>'success'));
	}

	/**
	 * 前端获取系统参数
	 */
	public function getSetting()
	{
		$_setting = $this->Setting->findOne(array('uniacid'=>$this->_uniacid),$this->_gpc['returns']);
		if($_setting) $this->_result = array('status'=>'success','data'=>$_setting);
		$this->result($this->_result);
	}

	/**
	 * 验证系统是否可以开启域名防封，是否安装了酷源防封模块
	 */
	public function checkDomainDefend()
	{
		if(pdo_tableexists('kuyuan_defend_domain')){
			$this->_result = array('status'=>'success');
		}
		$this->result($this->_result);
	}
}